// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.

package org.jetbrains.kotlin.idea.refactoring.cutPaste

import com.intellij.codeInsight.hint.HintManager
import com.intellij.codeInspection.HintAction
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.IdeActions
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.RangeMarker
import com.intellij.openapi.editor.asTextRange
import com.intellij.openapi.keymap.KeymapUtil
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiModificationTracker
import com.intellij.refactoring.BaseRefactoringIntentionAction
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle

class MoveDeclarationsIntentionAction(
    private val processor: MoveDeclarationsProcessor,
    private val bounds: RangeMarker,
    private val modificationCount: Long
) : BaseRefactoringIntentionAction(), HintAction {

    override fun startInWriteAction() = false

    override fun getText() = KotlinBundle.message("text.update.usages.to.reflect.declaration.0.move",
        processor.pastedDeclarations.size
    )

    override fun getFamilyName() = KotlinBundle.message("family.name.update.usages.on.declarations.cut.paste")

    override fun isAvailable(project: Project, editor: Editor, element: PsiElement): Boolean {
        return PsiModificationTracker.getInstance(processor.project).modificationCount == modificationCount
    }

    override fun invoke(project: Project, editor: Editor, element: PsiElement) {
        processor.performRefactoring()
    }

    override fun showHint(editor: Editor): Boolean {
        val range = bounds.asTextRange ?: return false
        if (editor.caretModel.offset != range.endOffset) return false

        if (PsiModificationTracker.getInstance(processor.project).modificationCount != modificationCount) return false

        val hintText = "$text? ${KeymapUtil.getFirstKeyboardShortcutText(
            ActionManager.getInstance().getAction(IdeActions.ACTION_SHOW_INTENTION_ACTIONS)
        )}"
        HintManager.getInstance().showQuestionHint(editor, hintText, range.endOffset, range.endOffset) {
            processor.performRefactoring()
            true
        }

        return true
    }
}